package tms.unitTests;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.TreeMap;


import static org.junit.Assert.*;


import org.junit.Before;
import org.junit.Test;

import tms.model.*;

/**
 * TaskTableUnitTests class contains the units tests of the methods in the TaskTable class 
 * 
 * @author Amer Alhalabi
 *
 */
public class TaskTableUnitTests {

	/// a header line used in writing the task table to a csv file
	private static final String HEADER_STRING = "ID,Title,Description,Duration,Deliverable,Deadline,Person\n";
	
	/**
	 * default constructor
	 */
	public TaskTableUnitTests() {
    }
	
	/**
	 * helper method to write a string to a file
	 * 
	 * @param fileName the name of the file to be written to 
	 * @param content content to be writen to the file
	 */
	private void createFile( String fileName, String content) {
		PrintWriter printOut = null; 
		try {
			printOut = new PrintWriter(fileName); 
			printOut.print(content); 
			printOut.close();
		}
		catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}
	
	/**
	 * helper method to check if a file exists
	 * 
	 * @param fileName name of the file to be checked 
	 * 
	 * @return true if the file exists; false otherwise
	 */
	private boolean fileExists(String fileName) {
		File file = new File(fileName);
		return file.exists();
	}
	
	/**
	 * helper method to delete a file
	 * 
	 * @param fileName the name of the file to be deleted
	 */
	private void deleteFile(String fileName) {
		File file = new File(fileName);
		file.delete();
	}
	
	/**
	 * helper method to load the content of a file into a string
	 * 
	 * @param filename name of the file whose content is to be loaded
	 * 
	 * @return a string containing the content of the file 
	 */
	private String loadFileContent(String filename) {
		byte[] buffer = new byte[(int) new File(filename).length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(filename));
	        f.read(buffer);
	    } catch (IOException e) {
			e.printStackTrace();
		} finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    return new String(buffer);
	}
	
	/**
	 * helper method called before every unit test to clear the tasktable and peopletable
	 */
	@Before
    public void setUp() {
        TaskTable.getInstance().getTaskMap().clear();
        TaskTable.getInstance().getTaskList().clear();
        PeopleTable.getInstance().getPersonList().clear();
    }
	
	/**
	 * test that the only instance of the task table is not null 
	 * and that it contains no person object
	 */
	@Test
	public void testTaskTableGetInstance1() {
       
		TaskTable taskTable = TaskTable.getInstance();
		assertNotNull(taskTable);
		assertTrue(taskTable.getTaskList().isEmpty());
    }
	
	/**
	 * test that the getInstance() always returns the same instance of the tasktable no matter how
	 * many times it is called
	 */
	@Test
	public void testTaskTableGetInstance2() {
       
		TaskTable taskTable1 = TaskTable.getInstance();
		TaskTable taskTable2 = TaskTable.getInstance();
		TaskTable taskTable3 = TaskTable.getInstance();
		
		assertNotNull(taskTable1);
		assertSame(taskTable1, taskTable2);
		assertSame(taskTable2, taskTable3);
    }
	
	/**
	 * test: the map of tasks and the list tasks in the tasktable contain nothing when the tasktable is empty
	 */
	@Test // test: empty map equals to empty list
	public void testTaskTableGetTaskList() {
		
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getTaskList().size();
		assertEquals(count1, count2);
		assertEquals(count1, 0);
	}
	
	/**
	 * test: the map of tasks and the list tasks in the tasktable contain
	 * the same task when the tasktable is contains one task
	 */
	@Test // test: a map with one item equals to a list with one item 
	public void testTaskTableGetTaskList2() {
		// create a task
		Task task = new Task();
		// add the task to the tasktable
		TaskTable.getInstance().addTask(task);
		
		// expect the sizes to be equal
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getTaskList().size();
		assertEquals(count1, count2);
		// expect the tasks in the map and in the list to be the same
		Task tmp1 = TaskTable.getInstance().getTaskMap().get(Integer.toString(task.getId()));
		Task tmp2 = TaskTable.getInstance().getTaskList().get(0);
		assertTrue(tmp1.equals(tmp2));
		assertTrue(tmp1.equals(task));
	}
	
	/**
	 * test: the map of tasks and the list tasks in the tasktable contain
	 * the same tasks when the tasktable is contains three tasks
	 */
	@Test  
	public void testTaskTableGetTaskList3() {
		// create three different tasks
		Task task1 = new Task();
		Task task2 = new Task();
		Task task3 = new Task();
		task1.setId(1);
		task2.setId(2);
		task3.setId(3);
		
		// add the tasks the tasktable
		TaskTable.getInstance().addTask(task1);
		TaskTable.getInstance().addTask(task2);
		TaskTable.getInstance().addTask(task3);
		
		// expect both the task map and task list to contain the same tasks
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getTaskList().size();
		assertEquals(count1, count2);
		assertEquals(count1, 3);
		assertTrue(TaskTable.getInstance().getTaskList().contains(task1));
		assertTrue(TaskTable.getInstance().getTaskList().contains(task2));
		assertTrue(TaskTable.getInstance().getTaskList().contains(task3));
	}
	
	/**
	 * test adding a null task object to the task table
	 * expect that the operation fails and no changes occur to the map of tasks
	 */
	@Test
	public void testTaskTableAddTask1() {
		// get the map of tasks before the attempt of the addition
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// add a null task to the tasktable
		TaskTable.getInstance().addTask(null);
		
		// get the map of tasks after the addition
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect no change to the map
		assertTrue(tasksBefore.equals(tasksAfter));
	}
	

	/**
	 * test adding a valid task object to the task table
	 * expect map of tasks has changed and now it contains the added task
	 */
	@Test
	public void testTaskTableAddTask2() {
		// get the map of tasks before the attempt of the addition
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// create a task and add it to the task table
		Task task = new Task();
		TaskTable.getInstance().addTask(task);
		
		// get the map of tasks after the addition
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect the task has been added to the map
		assertFalse(tasksBefore.equals(tasksAfter));
		Task tmp = TaskTable.getInstance().getTaskMap().get(Integer.toString(task.getId()));
		assertTrue(task.equals(tmp));
	}
	
	/**
	 * test adding an invalid task (invalid ID) object to the task table
	 * expect map of tasks has changed and now it contains the added task
	 * however expect the id of the task to be set to a default value (zero)
	 */
	@Test 
	public void testTaskTableAddTask3() {
		// get the map of tasks before the attempt of the addition
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		/// create a task with negative id and add it to the tasktable
		Task task = new Task();
		task.setId(-9);
		TaskTable.getInstance().addTask(task);
		
		// get the map of tasks after the addition
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		 // expect default task id is zero
		assertEquals(task.getId(), 0);
		
		// expect the task has been added to the map
		assertFalse(tasksBefore.equals(tasksAfter));
		Task tmp = TaskTable.getInstance().getTaskMap().get(Integer.toString(task.getId()));
		int count = TaskTable.getInstance().getTaskMap().size();
		assertTrue(task.equals(tmp));
		assertEquals(count , 1);
	}
	
	/**
	 * test adding two tasks to the task table; one with invalid ID (negarive) and the other is set to zero
	 * expect the id of the invalid task to be set to a default value (zero)
	 * which means that two tasks with the same id will be added to the task table
	 * 
	 * expect map of tasks has changed and that it contains only one task; the one first added
	 * 
	 */
	@Test // add two task; one with id=0, and one with invalid id
	public void testTaskTableAddTask4() {
		
		// get the map of tasks before the attempt of the addition
		TreeMap<String, Task> tasksBefore = TaskTable.getInstance().getTaskMap();
		
		// create a task with invalid id 
		Date deadline = Calendar.getInstance().getTime();
		Task task1 = new Task();
		task1.setId(-9); // default task id is zero
		task1.setDeadline(deadline);
		
		// create a second task with id and add it to the task table
		Task task2 = new Task();
		task2.setId(0);
		task2.setDeadline(deadline);
		
		// add the first task to the task table
		TaskTable.getInstance().addTask(task1);
		
		// get the map of tasks after the addition of the first task
		TreeMap<String, Task> tasksMid = TaskTable.getInstance().getTaskMap();
		
		// add the second task to the task table
		TaskTable.getInstance().addTask(task2);
		
		// get the map of tasks after the addition of the second task
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect that the tasks have the same id
		assertEquals(task1.getId(), task2.getId());
		assertTrue(task1.equals(task2));
		
		// expect the successful addition of the first task
		assertFalse(tasksBefore.equals(tasksMid));
		assertFalse(tasksBefore.equals(tasksAfter));
		
		// expect the unsuccessful addition of the second task
		assertTrue(tasksAfter.equals(tasksMid));
		int count = TaskTable.getInstance().getTaskMap().size();
		assertEquals(count , 1);
		Task tmp = TaskTable.getInstance().getTaskMap().get(Integer.toString(task1.getId()));
		assertTrue(task1.equals(tmp));
	}
	
	/**
	 * test adding the same task twice to the task table
	 * expect map of tasks has changed and that it contains only one copy of the task
	 * 
	 */
	@Test // add the same task twice
	public void testTaskTableAddTask5() {
		
		// get the map of tasks before the attempt of the addition
		TreeMap<String, Task> tasksBefore = TaskTable.getInstance().getTaskMap();
		
		// create a task
		Task task = new Task();
		task.setId(10);
		
		// add the task to the task table
		TaskTable.getInstance().addTask(task);
		
		// get the map of tasks after the first addition of the task
		TreeMap<String, Task> tasksMid = TaskTable.getInstance().getTaskMap();
		
		// add the same task again to the task table
		TaskTable.getInstance().addTask(task);
		
		// get the map of tasks after the second addition of the task
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect the successful first addition of the task
		assertFalse(tasksBefore.equals(tasksMid));
		assertFalse(tasksBefore.equals(tasksAfter));
		
		// expect the unsuccessful of the second addition of the task
		assertTrue(tasksAfter.equals(tasksMid));
		int count = TaskTable.getInstance().getTaskMap().size();
		assertEquals(count , 1);
		Task tmp = TaskTable.getInstance().getTaskMap().get(Integer.toString(task.getId()));
		assertTrue(task.equals(tmp));
	}
	
	/**
	 * test adding the three different tasks to the task table
	 * expect map of tasks has changed and that it contains three task
	 */
	@Test 
	public void testTaskTableAddTask6() {
		
		// get the map of tasks before the attempt of the addition
		TreeMap<String, Task> tasksBefore = TaskTable.getInstance().getTaskMap();
		
		// create three tasks
		Task task1 = new Task();
		Task task2 = new Task();
		Task task3 = new Task();
		task1.setId(1);
		task2.setId(2);
		task3.setId(3);
		
		// add the first task the tasktable
		TaskTable.getInstance().addTask(task1);
		
		// get the map of tasks after the addition of the first task
		TreeMap<String, Task> tasksMid1 = TaskTable.getInstance().getTaskMap();
		
		// add the second task the tasktable
		TaskTable.getInstance().addTask(task2);
		
		// get the map of tasks after the addition of the second task
		TreeMap<String, Task> tasksMid2 = TaskTable.getInstance().getTaskMap();
		
		// add the third task the tasktable
		TaskTable.getInstance().addTask(task3);
		
		// get the map of tasks after the addition of the third task
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect the map of tasks to contain all three tasks
		assertFalse(tasksBefore.equals(tasksMid1));
		assertFalse(tasksMid1.equals(tasksMid2));
		assertFalse(tasksMid2.equals(tasksAfter));
		assertFalse(tasksBefore.equals(tasksAfter));
		
		int count = TaskTable.getInstance().getTaskMap().size();
		assertEquals(count , 3);
	}
	
	/**
	 * test removing a null task from the task table.
	 * expect no change to the task table
	 */
	@Test 
	public void testTaskTableRemoveTask1() {
		
		// get the map of tasks before the attempt of the removal
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// remove a null task from the task table
		TaskTable.getInstance().removeTask(null);
		
		// get the map of tasks after the removal
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect no change to the tasktable
		assertTrue(tasksBefore.equals(tasksAfter));
	}
	
	/**
	 * test removing a task from the task table, but the task does not exist in the tasktable
	 * expect no change to the task table
	 */
	@Test 
	public void testTaskTableRemoveTask2() {
		
		// get the map of tasks before the attempt of the removal
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// create a task and try to remove it from the task table
		Task task1 = new Task();
		task1.setId(1);
		TaskTable.getInstance().removeTask(task1);
		
		// get the map of tasks after the attempt of the removal
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect no change to the tasktable
		assertTrue(tasksBefore.equals(tasksAfter));
	}

	/**
	 * test removing one task from the task table that has only that task
	 * expect the tasktable to be empty after the removal
	 */
	@Test 
	public void testTaskTableRemoveTask3() {
		
		// get the map of tasks before the attempt of the removal
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// create a task and add it to the task table
		Task task = new Task();
		task.setId(1);
		TaskTable.getInstance().addTask(task);
		
		// remove the task from the tasktable
		TaskTable.getInstance().removeTask(task);
		
		// get the map of tasks after the attempt of the removal
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect the tasktable to be empty
		assertTrue(tasksBefore.equals(tasksAfter));
		int count = TaskTable.getInstance().getTaskMap().size();
		assertEquals(count , 0);
		assertTrue(TaskTable.getInstance().getTaskMap().isEmpty());
	}
	
	/**
	 * test removing one task from the task table that has many tasks
	 * expect the tasktable to be NON-empty after the removal, and it does not contain the removed task 
	 */
	@Test
	public void testTaskTableRemoveTask4() {
		
		// get the map of tasks before the attempt of the removal
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// create three tasks and add them to the task table
		Task task1 = new Task();
		Task task2 = new Task();
		Task task3 = new Task();
		task1.setId(1);
		task2.setId(2);
		task3.setId(3);
		TaskTable.getInstance().addTask(task1);
		TaskTable.getInstance().addTask(task2);
		TaskTable.getInstance().addTask(task3);
		
		// get the map of tasks after the addition of the tasks
		TreeMap<String, Task> tasksMid= TaskTable.getInstance().getTaskMap();
		
		// remove task1
		TaskTable.getInstance().removeTask(task1);
		
		// get the map of tasks after the attempt of the removal
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect the tasktable to contain only task2 and task3
		assertFalse(tasksBefore.equals(tasksMid));
		assertFalse(tasksMid.equals(tasksAfter));
		assertFalse(tasksBefore.equals(tasksAfter));
		int count = TaskTable.getInstance().getTaskMap().size();
		assertEquals(count , 2);
		assertNull(TaskTable.getInstance().getTaskMap().get(Integer.toString(task1.getId())));
	}
	
	/**
	 * test removing the same task twice from the task table that has many tasks
	 * expect the tasktable to be NON-empty after the removal, and it does not contain the removed task
	 * and that the removal of the same task twice did not impact other tasks 
	 */
	@Test // remove the same task twice
	public void testTaskTableRemoveTask5() {
		
		TreeMap<String, Task> tasksBefore= TaskTable.getInstance().getTaskMap();
		
		// create three different tasks and add them to the task table
		Task task1 = new Task();
		Task task2 = new Task();
		Task task3 = new Task();
		task1.setId(1);
		task2.setId(2);
		task3.setId(3);
		TaskTable.getInstance().addTask(task1);
		TaskTable.getInstance().addTask(task2);
		TaskTable.getInstance().addTask(task3);
		TreeMap<String, Task> tasksMid1 = TaskTable.getInstance().getTaskMap();
		
		// remove task1
		TaskTable.getInstance().removeTask(task1);
		TreeMap<String, Task> tasksMid2 = TaskTable.getInstance().getTaskMap();
		
		// remove task1 again
		TaskTable.getInstance().removeTask(task1);
		TreeMap<String, Task> tasksAfter= TaskTable.getInstance().getTaskMap();
		
		// expect that the tasktable does not cotnain task1 and that it contains task2 and task3
		assertFalse(tasksBefore.equals(tasksMid1));
		assertFalse(tasksMid1.equals(tasksMid2));
		assertFalse(tasksBefore.equals(tasksAfter));
		assertTrue(tasksMid2.equals(tasksAfter));
		int count = TaskTable.getInstance().getTaskMap().size();
		assertEquals(count , 2);
		assertNull(TaskTable.getInstance().getTaskMap().get(Integer.toString(task1.getId())));
		assertNotNull(TaskTable.getInstance().getTaskMap().get(Integer.toString(task2.getId())));
		assertNotNull(TaskTable.getInstance().getTaskMap().get(Integer.toString(task3.getId())));
	}
	
	/**
	 * test getting the personlist from the task table that is empty
	 * expect the personlist to be empty
	 */
	@Test
	public void testTaskTableGetPersonList1() {
		
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getPersonlist().size();
		assertEquals(count1, count2);
	}
	
	/**
	 * test getting the personlist from the task table that contains one task
	 * expect the personlist to contain one person whose name is the same as the assignee of the task
	 */
	@Test 
	public void testTaskTableGetPersonList2() {
		
		// create a task and add it the task table
		Task task = new Task();
		task.setPerson("jack jones");
		TaskTable.getInstance().addTask(task);
		
		// expect the personlist to contain one item
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getPersonlist().size();
		assertEquals(count1, count2);
		
		// expect the task that assigned to the person is the one that was added 
		Task tmp1 = TaskTable.getInstance().getTaskMap().get(Integer.toString(task.getId()));
		Task tmp2 = TaskTable.getInstance().getPersonlist().get(0).getTasks().get(0);
		assertTrue(tmp1.equals(tmp2));
		assertTrue(tmp1.equals(task));
	}
	
	/**
	 * test getting the personlist from the task table that contains three tasks
	 * expect the personlist to contain three person objects whose names are the same as the assignees of the tasks
	 */
	@Test 
	public void testTaskTableGetPersonList3() {
		// create three different tasks
		Task task1 = new Task();
		Task task2 = new Task();
		Task task3 = new Task();
		task1.setId(1);
		task2.setId(2);
		task3.setId(3);
		task1.setPerson("jack jones");
		task2.setPerson("sam micheal");
		task3.setPerson("tony amra");
		
		// add the tasks to the task table
		TaskTable.getInstance().addTask(task1);
		TaskTable.getInstance().addTask(task2);
		TaskTable.getInstance().addTask(task3);
		
		// expect the personlist to contain three person objects whose assgined tasks are those ones 
		// added to the task table
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getPersonlist().size();
		assertEquals(count1, count2);
		assertEquals(count1, 3);
		Person person1 = TaskTable.getInstance().getPersonlist().get(0);
		Person person2 = TaskTable.getInstance().getPersonlist().get(1);
		Person person3 = TaskTable.getInstance().getPersonlist().get(2);
		assertEquals(person1.getTasks().size(), 1);
		assertEquals(person2.getTasks().size(), 1);
		assertEquals(person3.getTasks().size(), 1);
		assertTrue (person1.getName() == task1.getPerson() 
				   || person1.getName() == task2.getPerson()
				   || person1.getName() == task3.getPerson() );
		assertTrue (person2.getName() == task1.getPerson() 
				   || person2.getName() == task2.getPerson()
				   || person2.getName() == task3.getPerson() );
		assertTrue (person3.getName() == task1.getPerson() 
				   || person3.getName() == task2.getPerson()
				   || person3.getName() == task3.getPerson() );
	}
	
	/**
	 * test getting the personlist from the task table that contains three tasks; two of which
	 * are assigned to the same person.
	 * expect the personlist to contain two person objects; one person with task and another person with two tasks
	 */
	@Test  
	public void testTaskTableGetPersonList4() {
		
		// create three tasks; and assign two of which to the same person
		Task task1 = new Task();
		Task task2 = new Task();
		Task task3 = new Task();
		task1.setId(1);
		task2.setId(2);
		task3.setId(3);
		task1.setPerson("jack jones");
		task2.setPerson("sam micheal");
		task3.setPerson("jack jones");
		
		// add the tasks to the task table
		TaskTable.getInstance().addTask(task1);
		TaskTable.getInstance().addTask(task2);
		TaskTable.getInstance().addTask(task3);
		
		//expect that there are three tasks in the task Map
		//expect that there are two persons in the personList
		int count1 = TaskTable.getInstance().getTaskMap().size();
		int count2 = TaskTable.getInstance().getPersonlist().size();
		assertFalse(count1 == count2);
		assertEquals(count1, 3);
		assertEquals(count2, 2);
		
		// expect that the right person is assigned the right task(s)
		Person person1 = TaskTable.getInstance().getPersonlist().get(0);
		Person person2 = TaskTable.getInstance().getPersonlist().get(1);
		if (person1.getName().equals("jack jones") )
			assertEquals(person1.getTasks().size(), 2);
		else
			assertEquals(person2.getTasks().size(), 1);
		assertTrue (person1.getName() == task1.getPerson() || person1.getName() == task2.getPerson());
		assertTrue (person2.getName() == task1.getPerson() || person2.getName() == task2.getPerson());
	}
	
	/**
	 * test loading description of tasks from an empty xml file
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test
	public void testTaskTableOpenFile1() {
		
		String filename = "test.xml";
		String content = "";
		// create an empty input xml file
		createFile(filename, content);
		
		// expect failure 
		TaskTable taskTable = TaskTable.getInstance();
		PeopleTable peopleTable = PeopleTable.getInstance();
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect empty tables
		assertTrue(taskTable.getTaskList().isEmpty());
		assertTrue(peopleTable.getPersonList().isEmpty());
		deleteFile(filename);
	}
	
	/**
	 * test loading description of tasks from a file that does not exist
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test // pass a file that does not exist
	public void testTaskTableOpenFile2() {
		
		// try to load a file that does not exist
		// expect failure 
		String filename = "doesNotExist.xml";
		TaskTable taskTable = TaskTable.getInstance(); 
		PeopleTable peopleTable = PeopleTable.getInstance();
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect empty tables
		assertTrue(taskTable.getTaskList().isEmpty());
		assertTrue(peopleTable.getPersonList().isEmpty());
	}
	

	/**
	 * test loading description of tasks from a file name that is a empty string
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test
	public void testTaskTableOpenFile3() {
		
		// empty filename
		String filename = "";
		TaskTable taskTable = TaskTable.getInstance(); 

		// try to load the file
		// expect failure 
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect empty tables
		assertEquals(taskTable.getTaskList().size(), 0);
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 0);
		
	}
	
	/**
	 * test loading description of tasks from a file name that is null
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test 
	public void testTaskTableOpenFile4() {
	
		String filename = null;
		TaskTable taskTable = TaskTable.getInstance(); 
		
		// try to load the file
		// expect failure 
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect empty tables
		assertEquals(taskTable.getTaskList().size(), 0);
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 0);
	}
	
	/**
	 * test loading description of tasks from a file that contains one task
	 * expect a successful operation and that the task table contains one task (equal to the one in the file)
	 * expect  people table contain one person whose name is the same as the task's person name in the file 
	 */
	@Test 
	public void testTaskTableOpenFile5() {
		// create a file
		String filename = "test.xml";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		content += "\t\t<title>Iteration 1</title>\n";
		content += "\t\t<description>The first iteration of the project, including requirements document and source code</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>Requirements document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		content += "</data>\n"; 
		createFile(filename, content);
		
		// load the xml file
		TaskTable taskTable = TaskTable.getInstance(); 
		assertTrue(taskTable.openXMLFile(filename));
		
		// expect the task table to contain one task
		assertEquals(taskTable.getTaskList().size(), 1);
		// expect the people table to contain one person
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 1);
		
		// make sure that the task loaded into the task table is equal to the one in the file
		assertTrue(taskTable.getTaskMap().get("1").getTitle().equals("Iteration 1"));
		assertTrue(taskTable.getTaskMap().get("1").getDescription().equals("The first iteration of the project, including requirements document and source code"));
		assertTrue(taskTable.getTaskMap().get("1").getDuration() == 2);
		assertTrue(taskTable.getTaskMap().get("1").getDeliverable().equals("Requirements document"));
		System.out.println(taskTable.getTaskMap().get("1").getDeadline().toString());
		assertTrue(taskTable.getTaskMap().get("1").getDeadline().toString().equals("Fri Jan 27 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("1").getPerson().equals("Person 1"));
		
		// clean up
		deleteFile(filename);
	}
	
	/**
	 * test loading description of tasks from a file that contains many tasks
	 * and that there are at least two tasks assigned to the same person
	 * 
	 * expect a successful operation and that the task table contains all tasks described in the file
	 * expect  people table contain all persons whose names are described in the file 
	 */
	@Test
	public void testTaskTableOpenFile6() {
		// create a file with three tasks
		String filename = "test.xml";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		content += "\t\t<title>Iteration 1</title>\n";
		content += "\t\t<description>des1</description>\n";
		content += "\t\t<duration>1</duration>\n";
		content += "\t\t<deliverable>del1</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>des2</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>del2</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>3</id>\n";
		content += "\t\t<title>Iteration 3</title>\n";
		content += "\t\t<description>des3</description>\n";
		content += "\t\t<duration>3</duration>\n";
		content += "\t\t<deliverable>del3</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		content += "</data>\n";  
		
		createFile(filename, content);
		
		// load the file
		TaskTable taskTable = TaskTable.getInstance(); 
		assertTrue(taskTable.openXMLFile(filename));
		
		// expect the task table to contain three tasks
		assertEquals(taskTable.getTaskList().size(), 3);
		
		//expect the people table to contain two persons
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 2);
		
		// verify that the tasks that were added to the task table have the same attributes described in the file
		assertTrue(taskTable.getTaskMap().get("1").getTitle().equals("Iteration 1"));
		assertTrue(taskTable.getTaskMap().get("1").getDescription().equals("des1"));
		assertTrue(taskTable.getTaskMap().get("1").getDuration() == 1);
		assertTrue(taskTable.getTaskMap().get("1").getDeliverable().equals("del1"));
		assertTrue(taskTable.getTaskMap().get("1").getDeadline().toString().equals("Fri Jan 27 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("1").getPerson().equals("Person 1"));
		
		assertTrue(taskTable.getTaskMap().get("2").getTitle().equals("Iteration 2"));
		assertTrue(taskTable.getTaskMap().get("2").getDescription().equals("des2"));
		assertTrue(taskTable.getTaskMap().get("2").getDuration() == 2);
		assertTrue(taskTable.getTaskMap().get("2").getDeliverable().equals("del2"));
		assertTrue(taskTable.getTaskMap().get("2").getDeadline().toString().equals("Fri Jan 27 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("2").getPerson().equals("Person 2"));
		
		assertTrue(taskTable.getTaskMap().get("3").getTitle().equals("Iteration 3"));
		assertTrue(taskTable.getTaskMap().get("3").getDescription().equals("des3"));
		assertTrue(taskTable.getTaskMap().get("3").getDuration() == 3);
		assertTrue(taskTable.getTaskMap().get("3").getDeliverable().equals("del3"));
		assertTrue(taskTable.getTaskMap().get("3").getDeadline().toString().equals("Fri Jan 27 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("3").getPerson().equals("Person 1"));
		
		// clean up
		deleteFile(filename);
	}
	
	/**
	 * test loading description of tasks from a file that contains many tasks and subtasks
	 * and that there are at least two tasks assigned to the same person
	 * and that some tasks are assigned to nobody
	 * 
	 * expect a successful operation and that the task table contains all tasks and subtasks described in the file
	 * expect  people table contain all persons whose names are described in the file 
	 */
	@Test   
	public void testTaskTableOpenFile7() {
		// create a file
		String filename = "test.xml";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		content += "\t\t<title>Iteration 1</title>\n";
		content += "\t\t<description>The first iteration of the project, including requirements document and source code</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>Requirements document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>The second iteration of the project, including design document and source code</description>\n";
		content += "\t\t<duration>4</duration>\n";
		content += "\t\t<deliverable>Design document</deliverable>\n";
		content += "\t\t<deadline>2012-02-24</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t\t<subtasks>\n";
		content += "\t\t\t<subtask>\n";
		content += "\t\t\t\t<id>3</id>\n";
		content += "\t\t\t\t<title>Iteration 2.1</title>\n";
		content += "\t\t\t\t<description>21 description</description>\n";
		content += "\t\t\t</subtask>\n";
		content += "\t\t\t<subtask>\n";
		content += "\t\t\t\t<id>4</id>\n";
		content += "\t\t\t\t<title>Iteration 2.2</title>\n";
		content += "\t\t\t\t<description>22 description</description>\n";
		content += "\t\t\t\t<subtasks>\n";
		content += "\t\t\t\t\t<subtask>\n";
		content += "\t\t\t\t\t\t<id>5</id>\n";
		content += "\t\t\t\t\t\t<title>Iteration 2.2.1</title>\n";
		content += "\t\t\t\t\t\t<description>221 description</description>\n";
		content += "\t\t\t\t\t</subtask>\n";
		content += "\t\t\t\t\t<subtask>\n";
		content += "\t\t\t\t\t\t<id>6</id>\n";
		content += "\t\t\t\t\t\t<title>Iteration 2.2.2</title>\n";
		content += "\t\t\t\t\t\t<description>222 description</description>\n";
		content += "\t\t\t\t\t</subtask>\n";
		content += "\t\t\t\t</subtasks>\n";
		content += "\t\t\t</subtask>\n";
		content += "\t\t</subtasks>\n";
		content += "\t</task>\n";
		content += "\t<task>\n";
		content += "\t\t<id>7</id>\n";
		content += "\t\t<title>Iteration 3</title>\n";
		content += "\t\t<description>The third iteration of the project, including test plan, report document and source code</description>\n";
		content += "\t\t<duration>30</duration>\n";
		content += "\t\t<deliverable>Test plan and report document</deliverable>\n";
		content += "\t\t<deadline>2012-02-24</deadline>\n";
		content += "\t\t<person>Person 3</person>\n";
		content += "\t</task>\n";
		content += "\t<task>\n";
		content += "\t\t<id>8</id>\n";
		content += "\t\t<title>Iteration 4</title>\n";
		content += "\t\t<description>The final iteration of the project (complete system)</description>\n";
		content += "\t\t<duration>14</duration>\n";
		content += "\t\t<deliverable>Complete system</deliverable>\n";
		content += "\t\t<deadline>2012-02-24</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		content += "</data>\n";
		
		createFile(filename, content);
		
		// load the xml file
		TaskTable taskTable = TaskTable.getInstance();
		PeopleTable peopleTable = PeopleTable.getInstance();
		assertTrue(taskTable.openXMLFile(filename));
		
		// expect the task table to contain 8 tasks
		assertEquals(taskTable.getTaskList().size(), 8);
		
		// expect the people table to contain 4 persons
		// person1 is assinged two tasks
		// person2 is assigned one task
		// person3 is assigned one task
		// nobody is assinged four tasks
		assertEquals(peopleTable.getPersonList().size(), 4); // person1 to 3 + nobody
		
		// make sure the tasks and subtasks were added based on their IDs
		assertTrue(taskTable.getTaskMap().containsKey("1"));
		assertTrue(taskTable.getTaskMap().containsKey("2"));
		assertTrue(taskTable.getTaskMap().containsKey("3"));
		assertTrue(taskTable.getTaskMap().containsKey("4"));
		assertTrue(taskTable.getTaskMap().containsKey("5"));
		assertTrue(taskTable.getTaskMap().containsKey("6"));
		assertTrue(taskTable.getTaskMap().containsKey("7"));
		assertTrue(taskTable.getTaskMap().containsKey("8"));
		
		// verify that tasks and subtasks have valid info
		assertTrue(taskTable.getTaskMap().get("1").getTitle().equals("Iteration 1"));
		assertTrue(taskTable.getTaskMap().get("1").getDescription().equals("The first iteration of the project, including requirements document and source code"));
		assertTrue(taskTable.getTaskMap().get("1").getDuration() == 2);
		assertTrue(taskTable.getTaskMap().get("1").getDeliverable().equals("Requirements document"));
		assertTrue(taskTable.getTaskMap().get("1").getDeadline().toString().equals("Fri Jan 27 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("1").getPerson().equals("Person 1"));
		
		assertTrue(taskTable.getTaskMap().get("2").getTitle().equals("Iteration 2"));
		assertTrue(taskTable.getTaskMap().get("2").getDescription().equals("The second iteration of the project, including design document and source code"));
		assertTrue(taskTable.getTaskMap().get("2").getDuration() == 4);
		assertTrue(taskTable.getTaskMap().get("2").getDeliverable().equals("Design document"));
		assertTrue(taskTable.getTaskMap().get("2").getDeadline().toString().equals("Fri Feb 24 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("2").getPerson().equals("Person 2"));
		
		assertTrue(taskTable.getTaskMap().get("3").getTitle().equals("Iteration 2.1"));
		assertTrue(taskTable.getTaskMap().get("3").getDescription().equals("21 description"));
		assertTrue(taskTable.getTaskMap().get("3").getDuration() == 1);
		assertTrue(taskTable.getTaskMap().get("3").getDeliverable().equals("none"));
		assertTrue(taskTable.getTaskMap().get("3").getPerson().equals("nobody"));
		
		assertTrue(taskTable.getTaskMap().get("4").getTitle().equals("Iteration 2.2"));
		assertTrue(taskTable.getTaskMap().get("4").getDescription().equals("22 description"));
		assertTrue(taskTable.getTaskMap().get("4").getDuration() == 1);
		assertTrue(taskTable.getTaskMap().get("4").getDeliverable().equals("none"));
		assertTrue(taskTable.getTaskMap().get("4").getPerson().equals("nobody"));
		
		assertTrue(taskTable.getTaskMap().get("5").getTitle().equals("Iteration 2.2.1"));
		assertTrue(taskTable.getTaskMap().get("5").getDescription().equals("221 description"));
		assertTrue(taskTable.getTaskMap().get("5").getDuration() == 1);
		assertTrue(taskTable.getTaskMap().get("5").getDeliverable().equals("none"));
		assertTrue(taskTable.getTaskMap().get("5").getPerson().equals("nobody"));
		
		assertTrue(taskTable.getTaskMap().get("6").getTitle().equals("Iteration 2.2.2"));
		assertTrue(taskTable.getTaskMap().get("6").getDescription().equals("222 description"));
		assertTrue(taskTable.getTaskMap().get("6").getDuration() == 1);
		assertTrue(taskTable.getTaskMap().get("6").getDeliverable().equals("none"));
		assertTrue(taskTable.getTaskMap().get("6").getPerson().equals("nobody"));
		
		assertTrue(taskTable.getTaskMap().get("7").getTitle().equals("Iteration 3"));
		assertTrue(taskTable.getTaskMap().get("7").getDescription().equals("The third iteration of the project, including test plan, report document and source code"));
		assertTrue(taskTable.getTaskMap().get("7").getDuration() == 30);
		assertTrue(taskTable.getTaskMap().get("7").getDeliverable().equals("Test plan and report document"));
		assertTrue(taskTable.getTaskMap().get("7").getDeadline().toString().equals("Fri Feb 24 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("7").getPerson().equals("Person 3"));
		
		assertTrue(taskTable.getTaskMap().get("8").getTitle().equals("Iteration 4"));
		assertTrue(taskTable.getTaskMap().get("8").getDescription().equals("The final iteration of the project (complete system)"));
		assertTrue(taskTable.getTaskMap().get("8").getDuration() == 14);
		assertTrue(taskTable.getTaskMap().get("8").getDeliverable().equals("Complete system"));
		assertTrue(taskTable.getTaskMap().get("8").getDeadline().toString().equals("Fri Feb 24 00:00:00 EST 2012"));
		assertTrue(taskTable.getTaskMap().get("8").getPerson().equals("Person 1"));
		
		// clean up
		deleteFile(filename);
	}
	
	/**
	 * test loading description of tasks from a file that contains duplicate tasks (tasks with same id)
	 *  
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test
	public void testTaskTableOpenFile8() {
		// create a file
		String filename = "test.xml";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>what's up test</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>Requirements document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>The first iteration of the project, including requirements document and source code</description>\n";
		content += "\t\t<duration>8</duration>\n";
		content += "\t\t<deliverable>model document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t</task>\n";
		
		// dupilcate
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";   //a duplicate
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>cool</description>\n";
		content += "\t\t<duration>10</duration>\n";
		content += "\t\t<deliverable>Design document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		content += "</data>\n";  
		
		createFile(filename, content);
		
		TaskTable taskTable = TaskTable.getInstance(); 
		
		//load file
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect task table and people table to be empty
		assertEquals(taskTable.getTaskList().size(), 0);
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 0);
		
		// clean up
		deleteFile(filename);
	}
	
	/**
	 * test loading description of tasks from a file that contains tasks with missing attributes
	 *  
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test 
	public void testTaskTableOpenFile9() {
		// create a file
		String filename = "test.xml";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		// missing title
		content += "\t\t<description>what's up test</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>Requirements document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>The first iteration of the project, including requirements document and source code</description>\n";
		// missing duration
		content += "\t\t<deliverable>model document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>3</id>\n";   
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>cool</description>\n";
		content += "\t\t<duration>10</duration>\n";
		content += "\t\t<deliverable>Design document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		// missing person
		content += "\t</task>\n";
		content += "</data>\n"; 
		
		createFile(filename, content);
		
		// load file
		TaskTable taskTable = TaskTable.getInstance(); 
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect empty task and people tables 
		assertEquals(taskTable.getTaskList().size(), 0);
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 0);
		
		// clean up
		deleteFile(filename);
	}
	
	/**
	 * test loading description of tasks from a file that contains invalid xml format
	 *  
	 * expect an unsuccessful operation and that the task table and people table are empty 
	 */
	@Test
	public void testTaskTableOpenFile10() {
		// create a file
		String filename = "test.xml";
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</\n"; // invalid closing tag
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>what's up test</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>Requirements document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>The first iteration of the project, including requirements document and source code</description>\n";
		content += "\t\t<duration>8</duration>\n";
		content += "\t\t<deliverable>model document</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t</task>\n";
		
		createFile(filename, content);
		
		// load file
		TaskTable taskTable = TaskTable.getInstance(); 
		assertFalse(taskTable.openXMLFile(filename));
		
		// expect empty task and people tables 
		assertEquals(taskTable.getTaskList().size(), 0);
		assertEquals(PeopleTable.getInstance().getPersonList().size(), 0);
		
		//clean up
		deleteFile(filename);
	}
	
	/**
	 * test saving task table into a CSV file; where the filename is null
	 *  
	 * expect an unsuccessful operation and that no CSV file is created
	 */
	@Test
	public void testTaskTableSaveCSVFile1() {
		String filename = null;
		TaskTable taskTable = TaskTable.getInstance();
		assertFalse(taskTable.saveCSVFile(filename));
	}
	
	/**
	 * test saving task table into a CSV file; where the filename is an empty string
	 *  
	 * expect an unsuccessful operation and that no CSV file is created
	 */
	@Test
	public void testTaskTableSaveCSVFile2() {
		String filename = "";
		TaskTable taskTable = TaskTable.getInstance();
		assertFalse(taskTable.saveCSVFile(filename));
	}
	
	/**
	 * test saving task table into a CSV file; where the file already exist
	 *  
	 * expect an unsuccessful operation and that the content of the file is overwritten with the content of the task table
	 */
	@Test
	public void testTaskTableSaveCSVFile3() {
		
		// create an input xml file and an output csv file
		String infilename = "test.xml";
		String outFilename = "out.csv";
		
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		content += "\t\t<title>Iteration 1</title>\n";
		content += "\t\t<description>des1</description>\n";
		content += "\t\t<duration>1</duration>\n";
		content += "\t\t<deliverable>del1</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>des2</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>del1</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t</task>\n";
		content += "</data>\n";

		createFile(infilename, content);
		createFile(outFilename, "");  // create an output file
		
		// load the xml file
		TaskTable taskTable = TaskTable.getInstance();
		PeopleTable peopleTable = PeopleTable.getInstance();
		assertTrue(taskTable.openXMLFile(infilename));
		
		// expect the successful popluation of tasks and person
		assertEquals(taskTable.getTaskList().size(), 2);
		assertEquals(peopleTable.getPersonList().size(), 2);
		
		// save the tasktable into an CSV file
		assertTrue(taskTable.saveCSVFile(outFilename));
		assertTrue(fileExists(outFilename));
		
		// verify the content of the csv file
		String cont = null;
		cont = loadFileContent(outFilename);
		String expected = "ID,Title,Description,Duration,Deliverable,Deadline,Person\n" +
				"1,Iteration 1,des1,1,del1,Fri Jan 27 00:00:00 EST 2012,Person 1\n" +
				"2,Iteration 2,des2,2,del1,Fri Jan 27 00:00:00 EST 2012,Person 2\n";
		assertTrue(cont.equals(expected));
		
		deleteFile(infilename);
		deleteFile(outFilename);
	}
	
	/**
	 * test saving task table into a CSV file; when the task table is empty
	 *  
	 * expect a successful operation and that the content of the file is only a header describing the fields
	 */
	@Test
	public void testTaskTableSaveCSVFile4(){
		
		String outFilename = "out.csv";
		// make sure the task table is empty
		TaskTable taskTable = TaskTable.getInstance();
		PeopleTable peopleTable = PeopleTable.getInstance();
		assertEquals(taskTable.getTaskList().size(), 0);
		assertEquals(peopleTable.getPersonList().size(), 0);
		
		// save the task table to a CSB file
		assertTrue(taskTable.saveCSVFile(outFilename));
		assertTrue(fileExists(outFilename));
		
		// verify that the content is only the header
		String cont = loadFileContent(outFilename);
		assertTrue(cont.equals(HEADER_STRING));
		
		// clean up
		deleteFile(outFilename);
	}
	
	/**
	 * test saving a non-empty task table into a CSV file
	 *  
	 * expect a successful operation and that the content of the file contains a header describing the fields
	 * and the tasks
	 */
	@Test //invoke on a non-empty TaskTable
	public void testTaskTableSaveCSVFile5() {
		String infilename = "test.xml";
		String outFilename = "out.csv";
		
		// create input xml file
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		content += "<data>\n";
		content += "\t<task>\n";
		content += "\t\t<id>1</id>\n";
		content += "\t\t<title>Iteration 1</title>\n";
		content += "\t\t<description>des1</description>\n";
		content += "\t\t<duration>1</duration>\n";
		content += "\t\t<deliverable>del1</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 1</person>\n";
		content += "\t</task>\n";
		
		content += "\t<task>\n";
		content += "\t\t<id>2</id>\n";
		content += "\t\t<title>Iteration 2</title>\n";
		content += "\t\t<description>des2</description>\n";
		content += "\t\t<duration>2</duration>\n";
		content += "\t\t<deliverable>del1</deliverable>\n";
		content += "\t\t<deadline>2012-01-27</deadline>\n";
		content += "\t\t<person>Person 2</person>\n";
		content += "\t</task>\n";
		content += "</data>\n";
		createFile(infilename, content);
		
		// load the input xml file
		// verify the successful loading
		TaskTable taskTable = TaskTable.getInstance();
		PeopleTable peopleTable = PeopleTable.getInstance();
		assertTrue(taskTable.openXMLFile(infilename));
		assertEquals(taskTable.getTaskList().size(), 2);
		assertEquals(peopleTable.getPersonList().size(), 2);
		
		// save the task table to a CSV file
		assertTrue(taskTable.saveCSVFile(outFilename));
		assertTrue(fileExists(outFilename));
		
		// verify the content
		String cont = null;
		cont = loadFileContent(outFilename);
		String expected = "ID,Title,Description,Duration,Deliverable,Deadline,Person\n" +
				"1,Iteration 1,des1,1,del1,Fri Jan 27 00:00:00 EST 2012,Person 1\n" +
				"2,Iteration 2,des2,2,del1,Fri Jan 27 00:00:00 EST 2012,Person 2\n";
		assertTrue(cont.equals(expected));
		
		// clean up
		deleteFile(infilename);
		deleteFile(outFilename);
	}
}
